
/*
 * UploadFile.java
 *
 * Created on April 5, 2007, 5:17 PM
 *
 *
 */

package edu.byu.isys413.actions;

import Data.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileItem;

/**
 * See http://www.developershome.com/wap/wapUpload/wap_upload.asp?page=jsp
 * for a really good tutorial on JSP file upload using the Jakarta libarary.
 *
 * @author Dallin Regehr
 */
public class UploadFile implements edu.byu.isys413.web.Action{
    
    /** Creates a new instance of UploadFile */
    public UploadFile() {
    }
    public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        //CustomerBO cust = (CustomerBO)session.getAttribute("customer");
        CustomerBO cust = CustomerDAO.getInstance().read("123456789012345678901234567890");
        
        
        //System.out.println("Myname is (regular request object):" + request.getParameter("myname"));
        
        // parse the request using the Jakarta file upload library
        // convert the requestParams into a map.  this assumes that each form field on
        // the page has a unique name.  if two fields have the same name, the second
        // one will blast the first one
        ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
        List paramList = servletFileUpload.parseRequest(request);
        Map params = new TreeMap();
        for (int i = 0; i < paramList.size(); i++) {
            FileItem item = (FileItem)paramList.get(i);
            params.put(item.getFieldName(), item);
        }//for
        
        // get a regular parameter from the request
        //String myname = ((FileItem)params.get("myname")).getString();
        //System.out.println(myname);
        
        // get a file from the request
        FileItem fileinfo = (FileItem)params.get("myfile");
        System.out.println("File name: " + fileinfo.getName());
        System.out.println("File size: " + fileinfo.getSize());
        System.out.println("File type: " + fileinfo.getContentType());
        System.out.println("File fieldName: " + fileinfo.getFieldName());
        //System.out.println("File string: " + fileinfo.getString());
        System.out.println("File get...: " + fileinfo.get());
        System.out.println("File type: " + fileinfo.getInputStream());
        // the fileitem also has the bytes of the file.  you could easily call
        // setBinaryStream in your PreparedStatement since fileitem.getInputStream
        // gives you an input stream, which is what setBinaryStream wants.
        System.out.println(request.getParameterNames());
        //create the file and then save to database
        BackupFileBO file = BackupFileDAO.getInstance().create();
        file.setCustomerID(cust.getId());
        file.setFileName(fileinfo.getName());
        Date today = Calendar.getInstance().getTime();
        file.setDateAdded(today);
        file.setFileInStream(fileinfo.getInputStream());
        file.setSize(fileinfo.getSize());
        file.setExtension(fileinfo.getContentType());
        //save
        BackupFileDAO.getInstance().save(file);
        
        return "FileUpload2.jsp";
    }//process
    
}